<?php
namespace app\index\controller;
use think\Controller;
class User   extends Controller {

    public function login(){
        //判断是否登陆
         $user=session("o2o_user","","o2o");
        if($user && $user->id){
            $this->redirect(url("index/index"));
        }
        
        
        return $this->fetch("");
    }
    
    public  function register(){
          
        return $this->fetch("");
    }
    
    public function save(){
        if(!request()->isPost()){
            $this->error("非法访问");
        }
        $data=input("post.");
        //print_r($data);exit();
        //用户验证
          $username=$data["username"];
          $password=$data["password"];
          if(!$username){
           return   showmiss(0,"用户名不能为空");
          }
          if(!$password){
            return   showmiss(0,"密码不能为空");
          }
          
        $resu=model("User")->get(["username"=>$data['username']]);
        if($resu){
           return showmiss(0,"用户已注册");
        }
        //邮箱验证
         $result=model("User")->get(["email"=>$data['email']]);
        if($result){
             return showmiss(0,"邮箱已注册");
        }
        
        //密码验证
        if($data["password"]!=$data['repasswd']){
            return showmiss(0,"密码和确认密码不一致");
        }
        //验证码不为空
        if(!$data['verifycode']){
            return showmiss(0,"请输入验证码");
        }
          //验证码校验
        if(!captcha_check($data['verifycode'])){
             return showmiss(0,"验证码不一致");
        }
        //密码加盐防止注入
        $data['code']=mt_rand(100,10000);
        $data['password']=md5($data['password'].$data['code']);
        //$data=12;
        try{
           $res=model("User")->add($data);
          }catch(\Exception $e){
            $this->error($e->getMessage());
         }
         //
         if($res){
               return showmiss(1,"注册成功");
           }else{
               return showmiss(0,"注册失败");
           }
        
    }
    
    public function logincheck(){
        if(!request()->isPost()){
            $this->error("非法请求");
        }
        
        $data=input("post.");
          $username=$data["username"];
          $password=$data["password"];
          if(!$username){
           return   showmiss(0,"用户名不能为空");
          }
          if(!$password){
            return   showmiss(0,"密码不能为空");
          }
        //用户名验证
        try{
        $user=model("User")->getUserByUsername($data['username']);
        //print_r($user);
        }catch(\Exception $e){
            $this->error($e->getMessage());
        }
        //用户名验证
       if(!$user || $user->status !=1){
            return showmiss(0,"该用户不存在");
        }
        //密码验证
        if(md5($data['password'].$user->code) !=$user->password){
         return showmiss(0,"密码不一致");
        }
        //验证码校验
        if(!captcha_check($data['verifycode'])){
             return showmiss(0,"验证码错误");
        }
        
        model("User")->updateById(["last_login_time"=>time()],$user->id);
        //存入session         
         session("o2o_user",$user, "o2o");
        
         return showmiss(1, "登陆成功");
    }
    
    //退出
    public function logout(){
        //清除作用域里所有的内容
        session(null,"o2o");
        $this->redirect(url("index/index"));
    }
    
    public function index(){
      $user=session("o2o_user","","o2o");
      if(!$user){
         $this->redirect(url("user/login"));
     }
     $userid=$user->id;
      //order订单
      //conpous团购券  改变status 已使用、未使用 
      //密码修改以及其他细节
     
    }
    
}

